package cn.cqautotest.cqviehelper.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import cn.cqautotest.cqviehelper.R;

import com.gyf.immersionbar.ImmersionBar;
import com.hjq.base.action.BundleAction;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.cqautotest.cqviehelper.ui.dialog.ToastDialog;

public abstract class BaseActivity extends AppCompatActivity implements BundleAction {

    protected final String TAG = getClass().getSimpleName();

    //当前Activity是否处于前台
    protected boolean isFront = false;
    protected Handler mHandler = new Handler();
    private Unbinder mBind;
    private ImmersionBar mImmersionBar;
    private long lastTime = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (hideTitleBar()) setTitleBarStyle();
        initLayout();
        mBind = ButterKnife.bind(this);
        int statusBarHeight = ImmersionBar.getStatusBarHeight(this);
        getLayoutContainer().setPadding(0, statusBarHeight, 0, 0);
        initPresenter();
        initView();
        initData();
        initEvent();
    }

    protected View getLayoutContainer() {
        return new View(this);
    }

    protected void initPresenter() {

    }

    protected void initEvent() {

    }

    public View getDecorView() {
        return getWindow().getDecorView();
    }

    protected void setTitleBarStyle() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.init();
    }

    /**
     * 初始化沉浸式状态栏
     */
    protected ImmersionBar createStatusBarConfig() {
        return mImmersionBar;
    }

    protected boolean hideTitleBar() {
        return true;
    }

    protected void initData() {

    }

    protected void initView() {

    }

    /**
     * 初始化布局
     */
    protected void initLayout() {
        if (getLayoutId() > 0) {
            setContentView(getLayoutId());
            initSoftKeyboard();
        }
    }

    /**
     * 初始化软键盘
     */
    protected void initSoftKeyboard() {
        // 点击外部隐藏软键盘，提升用户体验
        getContentView().setOnClickListener(v -> hideSoftKeyboard());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) mBind.unbind();
        if (mHandler != null) mHandler = null;
        release();
    }

    protected void release() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        isFront = true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        isFront = false;
    }

    public boolean isFront() {
        return isFront;
    }

    @Override
    public Bundle getBundle() {
        return getIntent().getExtras();
    }

    @Override
    public void finish() {
        hideSoftKeyboard();
        super.finish();
        overridePendingTransition(R.anim.activity_left_in, R.anim.activity_left_out);
    }

    /**
     * 如果当前的 Activity（singleTop 启动模式） 被复用时会回调
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        // 设置为当前的 Intent，避免 Activity 被杀死后重启 Intent 还是最原先的那个
        setIntent(intent);
    }

    /**
     * 获取当前 Activity 对象
     */
    protected BaseActivity getActivity() {
        return this;
    }

    /**
     * 和 setContentView 对应的方法
     */
    public ViewGroup getContentView() {
        return findViewById(Window.ID_ANDROID_CONTENT);
    }

    /**
     * startActivity 方法简化
     */
    public void startActivity(Class<? extends Activity> clazz) {
        startActivity(new Intent(this, clazz));
    }

    /**
     * 隐藏软键盘
     */
    private void hideSoftKeyboard() {
        // 隐藏软键盘，避免软键盘引发的内存泄露
        View view = getCurrentFocus();
        if (view != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager != null) {
                manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        }
    }

    protected abstract int getLayoutId();

    protected void checkIfReturn() {
        if (System.currentTimeMillis() - lastTime < 500) {
            finish();
        }else {
            lastTime = System.currentTimeMillis();
            new ToastDialog.Builder(this).setMessage("再按一次退出程序").create().show();
        }
    }
}
